<template>
  <div class="container">
    <div class="container-head flex_btw">
      <div class="head-title flex_x">
        <img class="head-icon" src="@/assets/images/add.png" alt="" />
        <span>新增</span>
      </div>
    </div>
    <div class="container-main">
      <el-form :model="formList" label-width="auto" size="large">
        <el-form-item label="角色名">
          <el-col :span="10">
            <el-input v-model="formList.name" placeholder="请输入预警名称"
          /></el-col>
        </el-form-item>

        <el-form-item label="备注">
          <el-col>
            <el-input v-model="formList.con" type="textarea" :rows="6" />
          </el-col>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="formList.status" size="large" />
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        v-resize="{ bottomOffset: 150 }"
        ref="tableRef"
        style="width: 100%"
        cell-class-name="table_cell_l"
        header-cell-class-name="table_head_l"
      >
        <el-table-column prop="role" label="分类" width="150">
          <template #default="scope">
            <el-checkbox
              v-model="scope.row.isOpen"
              @change="allCheckChange(scope.row)"
              >{{ scope.row.role }}</el-checkbox
            >
          </template>
        </el-table-column>
        <el-table-column prop="child" label="子分类">
          <template #default="scope">
            <el-checkbox-group
              v-model="scope.row.child"
              @change="checkChange(scope.row)"
            >
              <el-checkbox v-for="item in scope.row.cities" :value="item">{{
                item
              }}</el-checkbox>
            </el-checkbox-group>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex_c">
        <el-button size="large" @click="router.back()">取消</el-button>
        <el-button size="large" type="primary" @click="onSubmit"
          >新增</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { Plus, Minus } from "@element-plus/icons-vue";
const router = useRouter();
const params = ref("");
const formList = reactive({
  name: "",
  target: "指标单数",
  status: true,
});
const tableData = ref([
  {
    role: "配置",
    isOpen: true,
    isAll: true,
    child: ["Shanghai", "Beijing"],
    cities: ["Shanghai", "Beijing", "Guangzhou", "Shenzhen"],
  },
])

const allCheckChange = (item: any) => {
  item.child = item.isOpen ? item.cities : []
};
const checkChange = (item: any) => {
  item.isOpen = item.child.length === item.cities.length
};
const onSubmit = () => {
  console.log("submit!");
};
</script>
<style lang="scss" scoped></style>
